/**
 * 
 */
package info.niwota.webime.http;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author qiangli
 *
 */
public class Message {
	public final static String TYPE_TEXT_HTML = "text/html; charset=UTF-8";
	
	public final static String TYPE_APPLICATION = "application/*";

	public static final SimpleDateFormat RFC1123_DF = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");

	public String startLine;
	
	//
	public List<String> headers;
	
	public String allow;
	
	public String contentEncoding;

	public String contentLanguage;
	
	public int contentLength = -1;
	
	public String contentLocation;
	
	public String contentMD5;
	
	public String contentRange;
	
	public String contentType = "text/html; charset=UTF-8";
	
	public long Expires = -1;
	
	public long lastModified = -1;
	
	public long date = -1;
	
	//
	public String mimeType;
	
	public String charset;
	
	public Object content;

	public String cookies;
	
	public Message() {
	}
	
	protected static void log(String s) {
		System.out.println(s);
	}
	
	public static String formatRFC1123(long date) {
		String s = RFC1123_DF.format(new Date(date));
		return s;
	}
	
	public static Date parseRFC1123(String s) throws ParseException {
		return RFC1123_DF.parse(s);
	}
	
	protected static String readLine(InputStream is) {
		byte[] b = new byte[1];
		StringBuffer sb = new StringBuffer();

		while (true) {
			try {
				is.read(b);
				if (b[0] == '\r') {
					is.read(b);
					if (b[0] == '\n') {
						break;
					}
					sb.append("\r");
				}
				sb.append(new String(b));

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		log("|" + sb.toString() + "|");
		return sb.toString();
	}
}
